//
//  MenuControllerVC.swift
//  HHSwift
//
//  Created by 华宏 on 2020/2/6.
//  Copyright © 2020 huahong. All rights reserved.
//

import UIKit

class MenuControllerVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        title = "UIMenuController"
               
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       
        let menuController = UIMenuController.shared
           menuController.setTargetRect(self.view.bounds, in: self.view)
           
           let item1 = UIMenuItem(title: "复制", action: #selector(copyAction))
           let item2 = UIMenuItem(title: "剪切", action: #selector(cutAction))
           let item3 = UIMenuItem(title: "粘贴", action: #selector(pasteAction))
           menuController.menuItems = [item1,item2,item3]
           
           menuController.arrowDirection = .down
           menuController.setMenuVisible(true, animated: true)
    }

   @objc func copyAction()  {
    
        let paste = UIPasteboard.general
        paste.string = "复制 UIMenuController"
    
    }
    
    @objc func cutAction()  {
    
        let paste = UIPasteboard.general
        paste.string = "剪切 UIMenuController"
        title = nil
    
    }
    
    @objc func pasteAction()  {
       
           let paste = UIPasteboard.general
           title = paste.string
       
       }
    
    override var canBecomeFirstResponder: Bool{
        return true
    }
    
    
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        
        if action == #selector(copyAction) || action == #selector(cutAction) || action == #selector(pasteAction) {
            return true
        }
        
        return false
    }
}
